<?php
namespace Admin\Model;
use Think\Model;
class AccessModel extends Model {
	protected $autoCheckFields =false;



    /**
      +----------------------------------------------------------
     * 管理员列表
      +----------------------------------------------------------
     */
    public function adminList($firstRow = 0, $listRows = 20) {
        $list = M("Admin")->order('aid desc')->limit($firstRow,$listRows)->select();
        foreach ($list as $k => &$v) {
            $list[$k]['statusTxt'] = $v['status'] == 1 ? "启用" : "禁用";
             $hhh=M('auth_group_access')->where('uid='.$v['aid'])->find();

         $v['juese']=$hhh['group_id'];
            
     
   
        }
           
        return $list;
    }

    /**
      +----------------------------------------------------------
     * 添加管理员
      +----------------------------------------------------------
     */
    public function addAdmin() {
        // if (!is_email($_POST['email'])) {
        //     $this->error="邮件地址错误";
        //     return false;
        // }
        $datas = array();
        $M = M("Admin");
        $datas['username'] = trim($_POST['username']);
        if ($M->where("`username`='" . $datas['username'] . "'")->count() > 0) {
            $this->error="已经存在该账号";
            return false;
            
        }
        $datas['email'] = trim($_POST['email']);
        // if ($M->where("`email`='" . $datas['email'] . "'")->count() > 0) {
        //     $this->error="已经存在该邮箱";
        //     return false;
        // }
        $datas['pwd'] = encrypt(trim($_POST['pwd']));
        $datas['remark'] = trim($_POST['remark']);
        $datas['time'] = time();
        if ($hhh=$M->add($datas)) {
    
            return $hhh;
        } else {
            $this->error="添加新账号失败，请重试";
            return false;
            
        }
    }

    /**
      +----------------------------------------------------------
     * 修改管理员
      +----------------------------------------------------------
     */
    public function editAdmin() {
        $M = M("Admin");
        if (!empty($_POST['pwd'])) {
            $_POST['pwd'] = encrypt(trim($_POST['pwd']));
        } else {
            unset($_POST['pwd']);
        }
        $user_id = (int) $_POST['aid'];

  M('auth_group_access')->where('uid='.$user_id)->setfield('group_id',$_POST['id']);
 $res=$M->where('aid='.$user_id)->save($_POST);
        
        if ($res) {
        
            return true;
        } else {
            return false;
        }
    }

   

    /**
     +----------------------------------------------------------
     * 修改密码
     +----------------------------------------------------------
     */
    public function editPwd() {
    	$M = M("Admin");
    	if (!empty($_POST['oldpwd'])) {
    		$oldpwd= encrypt(trim($_POST['oldpwd']));
    		if ($M->where("pwd='" .$oldpwd ."'")->count() <1 ) {
    			$this->error="旧密码错误";
    			return false;
    		}	
    	} 
    	$myinfo= $_SESSION['my_info'];
    	$user_id = (int) $myinfo['aid'];
    	$data['aid']=$user_id;
    	$data['pwd']= encrypt(trim($_POST['newpwd']));
    	if ($M->where('aid='.$user_id)->save($data)) {	 
    		return true;
    	} else {
    		$this->error="修改密码失败";
    		return false;
    	}
    }
    

}

?>
